package com.lgj.tlias.controller;

import com.lgj.tlias.domain.dto.EmpDTO;
import com.lgj.tlias.domain.pojo.Emp;
import com.lgj.tlias.domain.pojo.Result;
import com.lgj.tlias.service.EmpService;
import com.lgj.tlias.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class EmpController {
    @Autowired
    public EmpService empService;
    @PostMapping("/login")
    public Result login(@RequestBody EmpDTO empdto){
        Emp emp = empService.login(empdto);
        //登录成功，生成token返回
        if(emp!=null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id",emp.getId());
            claims.put("username",emp.getUsername());
            claims.put("name",emp.getName());
            //生成token
            String token = JwtUtils.CreateJWT(claims);
            //和前端商量好，返回token，前端设置
            return Result.success(1, token);
        }
        return Result.error(0,"账号或者密码错误");
    }
}
